package com.home766.controller.admin;

import com.home766.dto.DictionaryDTO;
import com.home766.entity.Cases;
import com.home766.service.CasesService;
import com.home766.service.IDictionaryService;
import net.biz.Message;
import net.biz.Message.Type;
import net.biz.Page;
import net.biz.Pageable;
import net.biz.Setting;
import net.biz.controller.admin.BaseController;
import net.biz.entity.ProductCategory;
import net.biz.service.ProductCategoryService;
import net.biz.util.SettingUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.commons.CommonsMultipartFile;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/admin/cases")
public class CasesController extends BaseController {
	@Resource(name="casesServiceImpl")
	private CasesService casesService;
	@Resource(name = "productCategoryServiceImpl")
	private ProductCategoryService productCategoryService;
	@Resource(name="dictionaryServiceImpl")
	private IDictionaryService iDictionaryService;
	/***
	 * 列表查询
	 * @param cases
	 * @param pageable
	 * @param model
	 * @return
	 */
	@RequestMapping(value="/list",method=RequestMethod.GET)
	public String getCasesList(Cases cases, Pageable pageable, ModelMap model, String sort, String properties){
		List<ProductCategory> listPc = productCategoryService.findRoots();
		Page<Cases> page = casesService.getCasesList(cases, pageable,properties);
		List<DictionaryDTO> dictionaryList = iDictionaryService.searchChildren(33L);
		model.addAttribute("page",page);
		model.addAttribute("listPc",listPc);
		model.addAttribute("cases",cases);
		model.addAttribute("dictionaryList",dictionaryList);
		model.addAttribute("sort",sort);
		return "/admin/case/list";
	}
	/***
	 * 删除(批量，单个)
	 * @param id
	 * @param request
	 * @param model
	 * @param redirectAttributes
	 * @return
	 */
	@RequestMapping(value="/delete")
	public String delete(Long[] id, HttpServletRequest request, ModelMap model, RedirectAttributes redirectAttributes){
		if(id.length>0){
			for(Long i : id){
				casesService.delete(i);
				addFlashMessage(redirectAttributes,new Message(Type.success,"删除成功"));
			}
		}else{
			addFlashMessage(redirectAttributes,new Message(Type.error,"删除失败"));
		}
		return "redirect:list.jhtml";
	}
	/***
	 * 新增页面跳转
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping("/addPage")
	public String addPage(HttpServletRequest request, ModelMap model){
		List<ProductCategory> listPc = productCategoryService.findRoots();
		List<DictionaryDTO> dictionaryList = iDictionaryService.searchChildren(33L);
		model.addAttribute("listPc",listPc);
		model.addAttribute("dictionaryList",dictionaryList);
		return "/admin/case/add";
	}
	/***
	 * 新增案例
	 * @param file
	 * @param cases
	 * @param request
	 * @param response
	 * @param redirectAttributes
	 * @return
	 * @throws IOException
	 */
	@RequestMapping("/save")
	public String add(@RequestParam("uploadFile")CommonsMultipartFile file, Cases cases, HttpServletRequest request, HttpServletResponse response, RedirectAttributes redirectAttributes) throws IOException {
		//获取文件的信息：文件的名称   注解@RequestParam
		String path = request.getSession().getServletContext().getRealPath("/upload/image");
		Setting setting = SettingUtils.get();
		File files =new File(path);
		if  (!files .exists()  && !files .isDirectory()){
			//新建文件夹
			files.mkdir(); 
		}
		//2.输出流,
		FileOutputStream fileOutputStream = new FileOutputStream(path+"/"+file.getOriginalFilename());
		//3.输入流
		InputStream inputStream = file.getInputStream();
		//读写
		int b =0;
		while((b=inputStream.read())!=-1){
			fileOutputStream.write(b);
		}
		fileOutputStream.flush();
		fileOutputStream.close();
		inputStream.close();
		if(cases!=null){
			SimpleDateFormat dfs = new SimpleDateFormat("ddHHmmss");
			String nowDate = dfs.format(new Date());
			cases.setSn(nowDate);
			cases.setAddresssImg(setting.getUploadUrl()+"/"+file.getOriginalFilename());
			casesService.save(cases);
			addFlashMessage(redirectAttributes,new Message(Type.success,"新增成功"));
		}else{
			addFlashMessage(redirectAttributes,new Message(Type.error,"新增失败"));
		}
		return "redirect:list.jhtml";
	}
	/***
	 * 编辑页面跳转
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping("/edit")
	public String editPage(Long id, HttpServletRequest request, ModelMap model){
		Cases cases = casesService.find(id);
		List<ProductCategory> listPc = productCategoryService.findRoots();
		List<DictionaryDTO> dictionaryList = iDictionaryService.searchChildren(33L);
		model.addAttribute("listPc",listPc);
		model.addAttribute("cases",cases);
		model.addAttribute("dictionaryList",dictionaryList);
		return "/admin/case/edit";
	}
	/***
	 * 查看页面跳转
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping("/view")
	public String viewPage(Long id, HttpServletRequest request, ModelMap model){
		Cases cases = casesService.find(id);
		List<ProductCategory> listPc = productCategoryService.findRoots();
		List<DictionaryDTO> dictionaryList = iDictionaryService.searchChildren(33L);
		model.addAttribute("cases",cases);
		model.addAttribute("dictionaryList",dictionaryList);
		return "/admin/case/view";
	}
	/***
	 * 修改案例
	 * @param cases
	 * @param request
	 * @return
	 * @throws IOException
	 */
	@RequestMapping("/update")
	public String update(@RequestParam("uploadFile")CommonsMultipartFile file, Cases cases, HttpServletRequest request, RedirectAttributes redirectAttributes) throws IOException {
		Cases ca = casesService.find(cases.getId());
		Setting setting = SettingUtils.get();
		if(file.getSize()!=0){
			File files =new File(ca.getAddresssImg());
			if(files.exists()){
				files.delete();
			}
			//获取文件的信息：文件的名称   注解@RequestParam
			String path = request.getSession().getServletContext().getRealPath("/upload/image");
			//2.输出流,
			FileOutputStream fileOutputStream = new FileOutputStream(path+"/"+file.getOriginalFilename());
			//3.输入流
			InputStream inputStream = file.getInputStream();
			//读写
			int b =0;
			while((b=inputStream.read())!=-1){
				fileOutputStream.write(b);
			}
			fileOutputStream.flush();
			fileOutputStream.close();
			inputStream.close();
			ca.setAddresssImg(setting.getUploadUrl()+"/"+file.getOriginalFilename());
		}
		if(ca!=null){
			ca.setName(cases.getName());
			ca.setSex(cases.getSex());
			ca.setCity(cases.getCity());
			ca.setResidentialQuarters(cases.getResidentialQuarters());
			ca.setIsRelease(cases.getIsRelease());
			ca.setIsCompleted(cases.getIsCompleted());
			ca.setHouseType(cases.getHouseType());
			ca.setProductCategory(cases.getProductCategory());
			ca.setTheme(cases.getTheme());
			ca.setUserInfo(cases.getUserInfo());
			ca.setFeedBack(cases.getFeedBack());
			ca.setSchemeConfiguration(cases.getSchemeConfiguration());
			ca.setDesignNotes(cases.getDesignNotes());
			ca.setSceneDisplay(cases.getSceneDisplay());
			casesService.update(ca);
			addFlashMessage(redirectAttributes,new Message(Type.success,"修改成功"));
		}else{
			addFlashMessage(redirectAttributes,new Message(Type.error,"修改失败"));
		}
		return "redirect:list.jhtml";
	}
}
